﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using Telerik.Web.UI;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class TR_ScholarshipType : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        InitialData_BLL iniData = new InitialData_BLL();
        ITrainingScholarshipTypeBLL SchTypeBLL = new TrainingScholarshipTypeBLL();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataGrid();
            }
        }

        private void LoadDataGrid()
        {
            
                RadGrid2.DataSource = SchTypeBLL.GetFull();
                RadGrid2.DataBind();
                //DataSet ListSalaryCommand = iniData.ListScholarshipType("");
                //RadGrid2.DataSource = ListSalaryCommand.Tables[0];           
        }

        protected void RadGrid2_ItemCommand(object o, GridCommandEventArgs e)
        {
            if (e.CommandName == "AddData")
            {
                UpdateUI("AddNew");

            }
            else if (e.CommandName == "DataEdit")
            {
                txtScholaTypeSeq.Text = RadGrid2.Items[e.Item.ItemIndex]["scholar_type_seq"].Text;
                txtScholaType.Text = RadGrid2.Items[e.Item.ItemIndex]["scholar_type_descr"].Text; 
                rblCoutry.SelectedValue = RadGrid2.Items[e.Item.ItemIndex]["scholar_local"].Text;
                UpdateUI("Update");
            }
            else if (e.CommandName == "Delete")
            {
                int UserID = ssMng.EmpSeqI;
                DateTime dtNow = DateTime.Now;
                UpdateUI("Save");
                TrainingScholarshipTypeInfo info = new TrainingScholarshipTypeInfo();
                info.scholar_type_seq = Convert.ToInt32(RadGrid2.Items[e.Item.ItemIndex]["scholar_type_seq"].Text);
                info.updated_by = UserID;
                info.updated_when = dtNow;
                SchTypeBLL.Delete(info);
            }
            LoadDataGrid();
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int ScholaTypeSeq = Convert.ToInt32(txtScholaTypeSeq.Text);
            string strScholaType = txtScholaType.Text;
            int strCountry = Convert.ToInt32(rblCoutry.SelectedValue);
            int UserID = ssMng.EmpSeqI;
            DateTime dtNow = DateTime.Now;
            UpdateUI("Save");
            TrainingScholarshipTypeInfo info = new TrainingScholarshipTypeInfo();
            info.scholar_type_seq = ScholaTypeSeq;
            info.scholar_type_descr = strScholaType;
            info.scholar_local = strCountry;
            info.updated_by = UserID;
            info.updated_when = dtNow;
            SchTypeBLL.Update(info);
            LoadDataGrid();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (checkValidate() == true)
            {
                string strScholaType = txtScholaType.Text;
                int strCountry = Convert.ToInt32(rblCoutry.SelectedValue);
                int UserID = ssMng.EmpSeqI;
                DateTime dtNow = DateTime.Now;
                UpdateUI("Save");
                ClearControl();
                TrainingScholarshipTypeInfo info = new TrainingScholarshipTypeInfo();
                info.scholar_type_descr = strScholaType;
                info.scholar_local = strCountry;
                info.created_by = UserID;
                info.created_when = dtNow;
                SchTypeBLL.Insert(info);
                LoadDataGrid();
            }

        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            UpdateUI("Cancel");
            ClearControl();
        }
        protected void ClearControl()
        {
            rblCoutry.SelectedValue = "1";
            lblError.Text = "";
            txtScholaType.Text = "";
            txtScholaTypeSeq.Text = "";
        }
        protected bool checkValidate()
        {
            bool bOK = true;
            string strScholaType = txtScholaType.Text;
            if (strScholaType.Length == 0)
            {
                bOK = false;
                lblError.Text = "* กรุณาใส่แหล่งทุน";
            }                       
            return bOK;
        }
        protected void UpdateUI(string strCommand)
        {
            switch (strCommand)
            {
               case "Save":
                    PanelGrid.Visible = true;
                    PanelAddData.Visible = false;

                    break;

                case "AddNew":
                    PanelGrid.Visible = false;
                    PanelAddData.Visible = true;
                    btnUpdate.Visible = false;
                    btnSave.Visible = true;

                    break;
                case "Cancel":
                    PanelGrid.Visible = true;
                    PanelAddData.Visible = false;

                    break;

                case "Update":
                    PanelGrid.Visible = false;
                    PanelAddData.Visible = true;
                    btnSave.Visible = false;
                    btnUpdate.Visible = true;

                    break;
            }
        }

    }
}
